home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / async.zip / _CROOT.C next >
C/C++ Source or Header  |  1986-09-24  |  2KB  |  103 lines

  1. /* Copyright (C) 1981,1982, 1983 by Manx Software Systems */
  2. /* modified by Kent Williams to support file name wild card expansion */
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #ifndef NULL
  6. #define NULL ((void *)0)
  7. #endif
  8.  
  9. char *sbrk();
  10. void abort();
  11. #define ARGMAX 32
  12. static char *Argv[ARGMAX];
  13. static int argvsize;
  14. static int Argc;
  15.  
  16. noper()
  17. {
  18.     return 0;
  19. }
  20.  
  21. int (*cls_)() = noper;
  22. extern char _ioflg[];
  23.  
  24. Croot(cp, first)
  25. register char *cp;
  26. {
  27.     register char *cp2;
  28.  
  29.     _ioflg[0] = isatty(0);    /* set flag for i/o routines */
  30.     _ioflg[1] = isatty(1);    /* set flag for i/o routines */
  31.     _ioflg[2] = isatty(2);    /* set flag for i/o routines */
  32.  
  33.  
  34.     /* Null out first argument */
  35.     Argv[0] = "";
  36.     Argc = first;
  37.  
  38.     /* loop through arguments */
  39.     for (;;) 
  40.     {
  41.         /* skip blanks */
  42.         while (*cp == ' ' || *cp == '\t')
  43.             ++cp;
  44.  
  45.         /* if you're at the end of command line, you're done */
  46.         if (*cp == 0)
  47.             break;
  48.         /* if you hit a quote, next arg starts after matching close quote */
  49.         if (*cp == '"' || *cp == '\'')
  50.         {
  51.             char quotechar = *cp;
  52.             cp2 = ++cp; /* save ptr to the string after quote */
  53.             while(*++cp2)
  54.             {
  55.                 if (*cp2 == quotechar)
  56.                 {
  57.                     *cp2++ = 0;
  58.                     goto noexpand;    /* don't want to trip on wildcards */
  59.                 }
  60.             }
  61.         } else /* assume args terminated by white space */
  62.         {
  63.             /* find beginning of next argument */
  64.             cp2 = cp;    /* save original pointer to the string */
  65.             *cp2 = (*cp2 == '/' ? '\\' : *cp2);
  66.             while (*++cp2)
  67.             {
  68.                 /* if you hit a space char - stick a null in to terminate last
  69.                    argument
  70.                  */
  71.                 if (*cp2 == ' ' || *cp2 == '\t') 
  72.                 {
  73.                     *cp2++ = 0;
  74.                     break;
  75.                 }
  76.             }
  77.         }
  78. noexpand:
  79.         /* update the next argv pointer */
  80.         Argv[Argc] = cp;
  81.         /* bump the argument count */
  82.         if (++Argc == ARGMAX)
  83.             abort();
  84.         cp = cp2;    /* point to beginning of next argument */
  85.     }
  86.     Argv[Argc] = NULL;    
  87.     main(Argc,Argv);
  88.     exit(0);
  89. }
  90.  
  91. static void abort()
  92. {
  93.     write(2, "Too many args.", 14);
  94.     _exit(200);
  95. }
  96.  
  97. exit(code)
  98. {
  99.     (*cls_)();
  100.     _exit(code);
  101. }
  102.  
  103.